package es.timmps.luchynews;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import es.timmps.luchynews.daos.Noticia;


public class XMLParser {
	private URL url;
	private Context cont;
	public XMLParser(String url,Context c) {
		try {
			cont = c;
			Log.d("URL", url);
			this.url = new URL(url);
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
	
	public LinkedList<HashMap<String, String>> parse() {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		LinkedList<HashMap<String, String>> entries = new LinkedList<HashMap<String, String>>();
		HashMap<String, String> entry;
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document dom = builder.parse(this.url.openConnection().getInputStream());
			Element root = dom.getDocumentElement();
			NodeList items = root.getElementsByTagName("item");
			for (int i=0;i<items.getLength();i++){
				entry = new HashMap<String, String>();				
				Node item = items.item(i);
				NodeList properties = item.getChildNodes();
				for (int j=0;j<properties.getLength();j++){
					Node property = properties.item(j);
					String name = property.getNodeName();
					if (name.equalsIgnoreCase("title")){
						entry.put(Main.DATA_TITLE, property.getFirstChild().getNodeValue());
					} else if (name.equalsIgnoreCase("media:thumbnail")){
						entry.put(Main.DATA_LINK, property.getAttributes().getNamedItem("url").getTextContent());						
					}
				}
				entries.add(entry);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 

		return entries;
	}	
	public ArrayList<Noticia> getListNoticias() {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		ArrayList<Noticia> entries = new ArrayList<Noticia>();
		
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document dom = builder.parse(this.url.openConnection().getInputStream());
			Element root = dom.getDocumentElement();
			NodeList items = root.getElementsByTagName("item");
			
			for (int i=0;i<items.getLength();i++){
				int tamI = 999999999;
				Noticia entry = new Noticia();				
				Node item = items.item(i);
				NodeList properties = item.getChildNodes();
				for (int j=0;j<properties.getLength();j++){
					Node property = properties.item(j);
					String name = property.getNodeName();
					if (name.equalsIgnoreCase("title")){
						entry.setTitular(property.getFirstChild().getNodeValue());
					}  
					if (name.equalsIgnoreCase("media:thumbnail")){
						entry.setMiniatura(property.getAttributes().getNamedItem("url").getTextContent());						
					}
					if (name.equalsIgnoreCase("enclosure")){
						String type = property.getAttributes().getNamedItem("type").getTextContent();
						if(!type.equals("audio/mpeg")){
						int t = Integer.valueOf(property.getAttributes().getNamedItem("length").getTextContent());
						if(t < tamI){
						entry.setMiniatura(property.getAttributes().getNamedItem("url").getTextContent());						
							tamI = t;
						}
						}
					}
					if (name.equalsIgnoreCase("media:description")){
						entry.setDescripcion(property.getFirstChild().getNodeValue());						
					}
					if (name.equalsIgnoreCase("content:encode")){
						
						entry.setDescripcionHTML(property.getFirstChild().getNodeValue());						
					}
					if (name.equalsIgnoreCase("description")){
						entry.setDescripcion(property.getTextContent());
						Log.d("Descrpcion",property.getTextContent());
					}
					if (name.equalsIgnoreCase("link")){
						entry.setLink(property.getFirstChild().getNodeValue());						
					}
					if (name.equalsIgnoreCase("pubDate")){
						entry.setFechaPub(property.getFirstChild().getNodeValue());						
					}
				}
				entries.add(entry);
			}
		} catch (Exception e) {
			return entries;
		} 

		return entries;
	}		
}
